﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace phminarm.algorithm.DataProcess
{
    public class FindPeaks
    {
        public int[] go(float[] data, int peaknum)
        {
            return getPeaksIndex(trendSign(oneDiff(data.ToList()))).ToArray();
        }

        //第一次寻峰（基本峰距为1）算法
        private float[] oneDiff(List<float> data)
        {
            float[] result = new float[data.Count - 1];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = data[i + 1] - data[i];
            }
            return result;
        }

        private int[] trendSign(float[] data)
        {
            int[] sign = new int[data.Length];
            for (int i = 0; i < sign.Length; i++)
            {
                if (data[i] > 0) sign[i] = 1;
                else if (data[i] == 0) sign[i] = 0;
                else sign[i] = -1;
            }

            for (int i = sign.Length - 1; i >= 0; i--)
            {
                if (sign[i] == 0 && i == sign.Length - 1)
                {
                    sign[i] = 1;
                }
                else if (sign[i] == 0)
                {
                    if (sign[i + 1] >= 0)
                    {
                        sign[i] = 1;
                    }
                    else
                    {
                        sign[i] = -1;
                    }
                }
            }
            return sign;
        }

        private List<int> getPeaksIndex(int[] diff)
        {
            List<int> data = new List<int>();
            for (int i = 0; i != diff.Length - 1; i++)
            {
                if (diff[i + 1] - diff[i] == -2)
                {
                    data.Add(i + 1);
                }
            }
            return data;//相当于原数组的下标
        }
    }
}
